﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

namespace WebSite_CodeMan.Sys_CodeMan_Manage
{
    public partial class Admin_TMes_Update : System.Web.UI.Page
    {
        protected string ProImage = "../UpLoad/Images/NoImage.gif";
        protected void Page_Load(object sender, EventArgs e)
        {
            BaseClass.BaseClass.CheckState();
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            string strPID = Common.GetMes.GetRequestQuery(Request, "NID");
            if (BLL.NoteInfo.EditNoteMes(strPID, "", "", Server.HtmlEncode(txtTMes.Value.Trim()), "", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "", "0", HImageName.Value, ""))
            {
                if (string.IsNullOrEmpty(strPID))
                {
                    Common.MessageBox.myScriptMes(Page, "if(confirm(\"微博信息保存成功，是否继续添加？否则返回信息列表。\")) {location.href=\"Admin_TMes_Mes.aspx\"} else  {location.href=\"Admin_TMes_Mes.aspx\"}", "suess");

                }
                else
                {
                    Common.MessageBox.Show(Page, "信息保存成功......", "Admin_TMes_Mes.aspx", "error");
                }
            }
            else
            {
                Common.MessageBox.Show(Page, "信息保存失败，请重试或联系技术人员......", "error");
            }
        }
        private void LoadData()
        {

            string strNID = Common.GetMes.GetRequestQuery(Request, "NID");
            if (!string.IsNullOrEmpty(strNID))
            {
                OleDbDataReader DataRead = BLL.NoteInfo.GetAllNoteInfo(strNID);
                if (DataRead.HasRows)
                {
                    DataRead.Read();
                    txtTMes.Value = Server.HtmlDecode(DataRead["T_NoteDes"].ToString());
                    HImageName.Value = ((string.IsNullOrEmpty(DataRead["T_NoteImage"].ToString())) ? "NoImage.gif" : DataRead["T_NoteImage"].ToString());
                    ProImage = "../UpLoad/Images/Small/" + HImageName.Value;
                }
                DataRead.Close();
                DataRead.Dispose();
            }
        }

        protected void BtnUpLoad_Click(object sender, EventArgs e)
        {
            HImageName.Value = (HImageName.Value == "noImage.gif") ? Common.GetMes.GetNowTimeString() + UploadImg.FileName.Substring(UploadImg.FileName.LastIndexOf(".")) : HImageName.Value;
            switch (BaseClass.BaseClass.UpFile(Page, UploadImg, "img", HImageName.Value))
            {
                case 1:
                    string newsImageName = HImageName.Value;
                    string smallImageName = HImageName.Value.Substring(0, HImageName.Value.IndexOf("."));
                    BaseClass.BaseClass.MakeThumbnail(Server.MapPath("~/UpLoad/Images/" + newsImageName), Server.MapPath("~/UpLoad/Images/Small/" + smallImageName + ".jpg"),100, 110, "HW", "jpg");
                    ProImage = "../UpLoad/Images/Small/" + newsImageName;
                    break;
                case -1:
                    Common.MessageBox.Show(Page, "上传文件格式有误，限制使用gif，jpeg。", "e1");
                    break;
                case -2:
                    Common.MessageBox.Show(Page, "上传文件太大，请确保图片小于500K。", "e2");
                    break;
                default:
                    Common.MessageBox.Show(Page, "未知错误，请重试或联系管理人员。", "e3");
                    break;
            }
        }
    }
}
